﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MyTRCP.Common.Services
{
    public class DevAudioLinkService : DevBluetoothLinkService
    {
        public DevAudioLinkService(ILogger<DevAudioLinkService> logger, IConfigService ics) : base(logger, ics)
        {
            logger.LogDebug($"DevAudioLinkService creating...");
        }

        protected override Guid TargetService => new Guid("39144315-32fa-40db-85ed-fbfeba2d86e6");

        protected override string TargetServiceName => "音频";

        protected override void ProcessReceivedDataQueue()
        {
            while (_btStreamReadUnprocessDataQueue.Count > 0)
            {
                nowByte = _btStreamReadUnprocessDataQueue.Dequeue();
                if (payloadRecvUncomplete)
                {
                    uncompletePayload.Add(nowByte);
                    if (nowByte == 0x7e)
                    {
                        if (uncompletePayload.Count > 2)
                        {
                            payloadRecvUncomplete = false;
                            _btStreamReadCompletelyPayloadQueue.Add(uncompletePayload.ToArray());
                            uncompletePayload.Clear();
                        }
                        else
                        {
                            uncompletePayload.Clear();
                            uncompletePayload.Add(0x7e);
                        }
                    }
                }
                else
                {
                    if (nowByte == 0x7e)
                    {
                        payloadRecvUncomplete = true;
                        uncompletePayload.Add(nowByte);
                    }
                }
            }
        }
    }
}
